import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import { tbtheteacherisqualifiedforthecurriculum } from '../../../atoms/db';


const logger = anylogger('pages/api/teachgl/edit.api');

export type Result = {
	ok: true;
} | {
	ok: false;
	message: string;
};

export type Message = ITbtheteacherisqualifiedforthecurriculum;


/**
 * 编辑课程
 */
const handler = an48<Result>();

handler.post(async(req, res) => {
	try {
		logger.debug('msg body:', req.body);
		const {curriculum_id,teacher_id,... body } = req.body as Message;

		if (!body
				// || !body.trem_id
				|| !body.coursename
				|| !body.teachername
				|| !curriculum_id
				|| !teacher_id
				// || !body.trem_name
			) {
			res.status(200).json({
				ok: false,
				message: '缺少必要的参数'
			});
			return;
		}
		const course = await tbtheteacherisqualifiedforthecurriculum().first('*').where({
			curriculum_id: curriculum_id,
			teacher_id: teacher_id,
			coursename: body.coursename,
			teachername: body.teachername
		}).andWhereNot({
		curriculum_id,teacher_id
		});
		if (course) {
			res.status(200).json({ ok: false, message: '课程已存在' });
			return;
		}
		await tbtheteacherisqualifiedforthecurriculum().update({
			...body,


		}).where({curriculum_id,teacher_id});
		res.status(200).json({ ok: true });
	} catch (error) {
		logger.trace(error);
		res.status(200).json({ ok: false, message: (error as Error).message });
	}
});

export const config = {} as PageConfig;

export default handler;
